﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using CMS.Services;
using CMS.BL;

namespace CMS.UI
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            var domainService = (IDomain)DependencyResolver.Current.GetService(typeof(IDomain));

            string newsPath = domainService.GetSettingsValue(SettingsKey.adNewsPath),
                testimonialsPath = domainService.GetSettingsValue(SettingsKey.adTestimonialsPath),
                sitemapPath = domainService.GetSettingsValue(SettingsKey.adSitemapPath);

            routes.Clear();
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "account",
                url: "account",
                defaults: new { controller = "Website", action = "DisplayCustomerAccount" }
            );

            routes.MapRoute(
                name: "EditMyDetails",
                url: "account/editMyDetails",
                defaults: new { controller = "Website", action = "EditCustomerAccount" }
            );

            routes.MapRoute(
                name: "Admn",
                url: "Admn/{action}/{id}",
                defaults: new { controller = "Admn", action = "Home", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Preview",
                url: "Preview",
                defaults: new { controller = "Website", action = "Content" }
            );

            routes.MapRoute(
                name: "Website",
                url: "Website/{action}/{id}",
                defaults: new { controller = "Website", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Search",
                url: "search/" + "{keyword}",
                defaults: new { controller = "Website", action = "Search", keyword = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Login",
                url: "login",
                defaults: new { controller = "Website", action = "Login" }
            );

            routes.MapRoute(
                name: "Logout",
                url: "logout",
                defaults: new { controller = "Website", action = "Logout" }
            );

            routes.MapRoute(
                name: "Registration",
                url: "registration",
                defaults: new { controller = "Website", action = "Registration" }
            );

            routes.MapRoute(
                name: "passReminder",
                url: "pass-reminder",
                defaults: new { controller = "Website", action = "ForgottenPassword" }
            );

            routes.MapRoute(
                name: "Testimonials",
                url: testimonialsPath.Trim('/'),
                defaults: new { controller = "Website", action = "Testimonials" }
            );

            routes.MapRoute(
                name: "Sitemap",
                url: sitemapPath.Trim('/'),
                defaults: new { controller = "Website", action = "Sitemap" }
            );

            routes.MapRoute(
                name: "XMLSitemap",
                url: "sitemap.xml",
                defaults: new { controller = "Website", action = "XMLSitemap" }
            );

            routes.MapRoute(
                name: "BlogSearch",
                url: String.Format("{0}/{1}/{2}", newsPath.Trim('/'), SettingsManager.Blog.SearchUrl.Trim('/'), "{keyword}"),
                defaults: new { controller = "Website", action = "BlogSearch", keyword = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "BlogRss",
                url: String.Format("{0}/{1}", newsPath.Trim('/'), "feed"),
                defaults: new { controller = "Website", action = "GetBlogRss" }
            );

            routes.MapRoute(
                name: "Blog",
                url: newsPath.Trim('/'),
                defaults: new { controller = "Website", action = "Blog" }
            );

            routes.MapRoute(
                name: "BlogCategory",
                url: String.Format("{0}/{1}/{2}", newsPath.Trim('/'), SettingsManager.Blog.CategoryUrl.Trim('/'), "{name}"),
                defaults: new { controller = "Website", action = "BlogCategory", name = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "BlogTag",
                url: String.Format("{0}/{1}/{2}", newsPath.Trim('/'), SettingsManager.Blog.TagUrl.Trim('/'), "{name}"),
                defaults: new { controller = "Website", action = "BlogTag", name = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "BlogItem",
                url: String.Format("{0}/{1}/{2}/{3}", newsPath.Trim('/'), "{year}", "{month}", "{title}"),
                defaults: new { controller = "Website", action = "Blog", year = UrlParameter.Optional, month = UrlParameter.Optional, title = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Default",
                url: "{*values}",
                defaults: new { controller = "Website", action = "Content" }
            );

            //routes.MapRoute(
            //    name: "Default",
            //    url: "{controller}/{action}/{id}",
            //    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            //);
        }
    }
}